package com.tan.lbs.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 
 * Copyright by Zhensheng Tan only for Final Year Project Use.
 * Class description: An easy tool for getting the unique ID when insert into tables.
 * @author Zhensheng Tan 
 *
 */
public class UniqueID {
	private static UniqueID instance;
	private Connection conn = null;

	/**
	 * Private Constructor.
	 */
	private UniqueID() {
	}

	/**
	 * Singleton pattern, only allow one DBManager class in runtime.
	 * 
	 * @return DBConnectionManager
	 */
	public static synchronized UniqueID getInstance() {
		if (instance == null) {
			instance = new UniqueID();
		}
		return instance;
	}
	
	/**
	 * Get the unique ID from database. This depends specifically on Oracle.
	 * @return
	 */
	public static int getUniqueID() {
		Connection con = JDBCUtil.getInstance().getConnection();
		int id = 0;
		 String sql = "select ID_seq.nextval from dual";  
	        Statement stmt = null;
	        ResultSet rs = null;
	        try {
				stmt = con.createStatement();
		        rs = stmt.executeQuery(sql);
		        rs.next();
		        id =  rs.getInt(1);
				if (rs != null) rs.close();
				if (stmt != null) stmt.close();
				if (con != null) con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	        return id;
	}
	

}
